package com.warrior.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 *
 * Created by Shawn on 2017/4/17.
 */
public class CheckLoginFilter implements Filter {

    public void init(FilterConfig filterChain) throws ServletException {

    }

    public void doFilter(ServletRequest req,
                         ServletResponse resp,
                         FilterChain filterChain)
            throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        String userId = (String) request.getSession().getAttribute("userId");
        if ("".equals(userId) || null == userId) {
            response.sendRedirect(request.getContextPath() + "/login");  //返回重新登录界面
            return;
        }
        filterChain.doFilter(req, resp);
    }

    @Override
    public void destroy() {

    }

}